#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @Version : Python-
# @Author  : Shengji He
# @Email   : hsjbit@163.com
# @File    : BinaryTreePaths.py
# @Time    : 2020/9/4 10:32
# @Description:
from typing import List


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def binaryTreePaths(self, root: TreeNode) -> List[str]:
        """
        Given a binary tree, return all root-to-leaf paths.
        
        Note: A leaf is a node with no children.
        
        Example:
            Input:

               1
             /   \
            2     3
             \
              5

            Output: ["1->2->5", "1->3"]
        
        Explanation: All root-to-leaf paths are: 1->2->5, 1->3

        :param root:
        :return:
        """
        def construct_paths(root: TreeNode, path):
            if root:
                path += str(root.val)
                if not root.left and not root.right:
                    paths.append(path)
                else:
                    path += '->'
                    construct_paths(root.left, path)
                    construct_paths(root.right, path)

        paths = []
        construct_paths(root, '')
        return paths


if __name__ == '__main__':
    S = Solution()
    root = TreeNode(1)
    root.left = TreeNode(2)
    root.right = TreeNode(3)
    root.left.right = TreeNode(5)
    print(S.binaryTreePaths(root))
    print('done')
